<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="rules"
      label-width="70px"
      class="login-box"
    >
      <h1 class="login-title">项目后台管理系统</h1>

      <el-form-item label="账号" prop="userCard">
        <el-input
          prefix-icon="el-icon-user-solid"
          type="text"
          maxlength="18"
          placeholder="请输入账号"
          v-model="loginForm.userCard"
          style="width: 250px"
        />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          type="password"
          placeholder="请输入密码"
          v-model="loginForm.password"
          style="width: 250px"
          maxlength="10"
          minlength="6"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          :loading="loginLoading"
          type="primary"
          class="login-button"
          v-on:click="getCode('loginForm')"
        >
          <span v-if="!loginLoading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div class="mask" v-show="isShowSlide" @click.self="isShowSlide = false">
      <Verify
        class="slideContainer"
        type="4"
        @success="successVerify"
        @error="errorVerify"
        :showButton="false"
      ></Verify>
    </div>
  </div>
</template>

<script>
import Verify from "vue2-verify";
export default {
  name: "Login",
  components: {
    Verify,
  },
  data() {
    return {
      loginLoading: false,
      loginForm: {
        userCard: "",
        password: "",
      },
      rules: {
        userCard: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
      },
      isShowSlide: false,
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loginLoading = true;

          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$message.success("登录成功");
              this.$router.push("/").catch(() => {});
              this.loginLoading = true;
            })
            .catch(() => {
              this.loginLoading = false;
              return false;
            });
        } else {
          this.loginLoading = false;
          return false;
        }
      });
    },
    getCode(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isShowSlide = true;
        } else {
          return;
        }
      });
    },
    successVerify(obj) {
      this.isShowSlide = false;
      obj.refresh();
      this.login("loginForm");
    },
    errorVerify(obj) {
      obj.refresh();
    },
  },
};
</script>

<style>
.login {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: url("../assets/login.png");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}

.login-box {
  background-color: #fff;
  border: 1px solid #dcdfe6;
  width: 370px;
  margin: 15% auto;
  padding: 35px 35px 15px 35px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
  opacity: 0.9;
}

.login-title {
  text-align: center;
  margin: 0 auto 40px auto;
  color: #303133;
}

.login-button {
  width: 250px;
}

.mask {
  position: fixed;
  left: 0%;
  top: 0%;
  width: 100%;
  height: 100%;
  z-index: 100;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.5);
}
.mask .slideContainer {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  left: 40%;
  top: 30%;
}
</style>